package com.example.administrator.activity.Homework.Homework5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.activity.R;

public class Homework5CarrtonListActivity extends AppCompatActivity {
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework5_carrton_list);
        listView = findViewById(R.id.cartom);
        final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);

        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.name[position];
                Toast.makeText(Homework5CarrtonListActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        };
    }

     class MyAdapter extends BaseAdapter {
        int[] img = {R.drawable.pika, R.drawable.haizei, R.drawable.huoying, R.drawable.kenan, R.drawable.daojian, R.drawable.jiasu, R.drawable.yinghua, R.drawable.youxi};
         String[] name = {"神奇宝贝", "海贼王", "火影忍者", "名侦探柯南", "刀剑神域", "加速世界", "樱花庄的女孩", "游戏人生"};

        @Override
        public int getCount() {
            return name.length;
        }

        @Override
        public Object getItem(int position) {
            return name[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=new ViewHolder();
            if (convertView==null){
                convertView=View.inflate(Homework5CarrtonListActivity.this,R.layout.activity_homework5_carrton_list_item,null);
                viewHolder.imageView=convertView.findViewById(R.id.pika);
                viewHolder.textView=convertView.findViewById(R.id.pika_one);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(img[position]);
            viewHolder.textView.setText(name[position]);
            return convertView;
        }
    }

    private class ViewHolder {

        public ImageView imageView;
        public TextView textView;
    }
//

//            View view = View.inflate(Homework5CarrtonListActivity.this, R.layout.activity_homework5_carrton_list_item, null);
//            ImageView imageView = view.findViewById(R.id.activity_market_jd);
//            TextView textView = view.findViewById(R.id.activity_market_jd_text);
//            imageView.setImageResource(img[position]);
//            textView.setText(name[position]);
//            return view;

        }

